﻿#include "PopInfoMsgDialog.h"


PopInfoMsgDialog::PopInfoMsgDialog(QWidget* parent
	, const Qt::WindowFlags& f): QWidget{ parent,f } {
	area = new QScrollArea(this);
	boxLayout = new QVBoxLayout(this);
	boxLayout->addWidget(area);
	area->setWidgetResizable(true);
	areaBoxLayout = new QVBoxLayout( );
	areadWidget = new QWidget(area);
	areadWidget->setLayout(areaBoxLayout);
	area->setWidget(areadWidget);
	area->setWidgetResizable(true);
	// 设置 area
	area->setWidgetResizable(true);
	area->setWindowFlags(Qt::FramelessWindowHint | Qt::SubWindow | Qt::WindowStaysOnTopHint);
	QPushButton* histBtn = new QPushButton("确定");
	areaBoxLayout->addWidget(histBtn);
	connect(histBtn, &QPushButton::clicked, this, &PopInfoMsgDialog::hideMsgBox);
}

void PopInfoMsgDialog::hideMsgBox(bool flage) {
	area->hide( );
}

void PopInfoMsgDialog::appMsg(const QString& msg) {
	QLabel* msgLabel = new QLabel(msg, area);
	areaBoxLayout->addWidget(msgLabel);
}

void PopInfoMsgDialog::showMsg(const QString msg
	, QWidget* pramParent) {
	const QList<QObject*> list = area->children( );
	for( auto& obj : list )
		if( obj->inherits("QLabel") ) {
			QLabel* qobjectCast = qobject_cast<QLabel*>(obj);
			areaBoxLayout->removeWidget(qobjectCast);
			delete obj;
		}
	appMsg(msg);
	area->show( );
	QPoint pos;
	if( winMain != Q_NULLPTR ) {
		pos = winMain->pos( );
	} else if( pramParent != Q_NULLPTR )
		pos = pramParent->pos( );
	else if( this->parent( ) != Q_NULLPTR ) {
		QWidget* widget = qobject_cast<QWidget*>(this->parent( ));
		if( widget != Q_NULLPTR )
			pos = widget->pos( );
		else
			pos = this->pos( );
	} else
		pos = this->pos( );
	auto&& mainSize = this->size( ) / 2;
	QSize size = area->size( ) / 2;
	area->move(pos.x( ) + mainSize.width( ) - size.width( ), pos.y( ) + mainSize.height( ) - size.height( ));
}
